package calculation;

/**
 *
 * @author KieuOanh
 */
public class Butterworth {
    //
    public double omegaP;
    public double omegaS;
    public double Rp;
    public double As;
//
    public int N;
    public double omegaC;
    public double omegaC1;
    public double omegaC2;
//
    public double num;
    public double dem;
    public double x1;
    public double x2;

    public Butterworth(double omegaP, double omegaS, double As, double Rp) {
        this.omegaP = omegaP;
        this.omegaS = omegaS;
        this.As = As;
        this.Rp = Rp;

        //tinh N
        x1 = pow10(Rp / 10) - 1;
        x2 = pow10(As / 10) - 1;
        num = log10(x1 / x2);
        dem = 2 * log10(omegaP / omegaS);
        N = round(num / dem);
        System.out.println("n=" + N);
        //tinh omegaC
        omegaC1 = omegaP / root2N(x1, N);
        omegaC2 = omegaS / root2N(x2, N);
        omegaC = trim1((omegaC1 + omegaC2) / 2);

    }

    int round(double x) {
        return (int) Math.round(x);
    }

    double log10(double x) {
        return Math.log10(x);
    }

    double pow10(double x) {
        return Math.pow(10, x);
    }

    double root2N(double x, int N) {
        return Math.pow(x, 1 / (2 * N));
    }

    double trim1(double x) {
        return (int) (x * 10) / (double) 10;
    }
}
